package com.xxx.springboot.handler;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.google.common.eventbus.AsyncEventBus;
import com.google.common.eventbus.Subscribe;
import com.xxx.springboot.mapper.UserMapper;
import com.xxx.springboot.pojo.entity.User;

import lombok.extern.slf4j.Slf4j;

/**
 * EventBus处理类
 */
@Slf4j
@Component
public class EventBusHandler {
	
	@Autowired
	private UserMapper demoMapper;

	@Autowired
	AsyncEventBus asyncEventBus;

	// 注册EventBus
	@PostConstruct // 项目启动时执行一次
	public void init() {
		asyncEventBus.register(this);
	}

	/**
	 * EventBus处理
	 */
	@Subscribe // EventBus订阅者
	public void getUserFromEventBus(String username) {
		log.info("enter method getUserFromEventBus(). username:{}", username);
    	User user = demoMapper.getByName(username);
    	log.info("getUserFromEventBus.user:{}", user);
	}
	
}
